#!/bin/sh

die() {
   echo $1 >&2
   exit 1
}

if [ $# -lt 1 ]; then
   die "Usage: stop-sandbox PORTS"
fi

exit_status=0

for port in "$@"; do
   if ! [ -d "/tmp/$port" ]; then
      echo "MySQL test server on port $port does not exist."
      continue
   fi

   if [ -x "/tmp/$port/stop" ]; then
      /tmp/$port/stop
      exit_status=$((exit_status | $?))
   else
      echo "/tmp/$port is missing files:" >&2
      ls -la /tmp/$port >&2
   fi

   rm -rf /tmp/$port
   exit_status=$((exit_status | $?))
done

exit $exit_status
